package com.woniuxy01;

import java.io.*;
import java.util.Arrays;

/**
 * @author 作者
 * @create 2025-09-16 14:10
 */
public class Test03 {
    public static void main(String[] args) throws IOException {
        FileReader fr=new FileReader("d:\\aaa\\a.mp4");
        FileWriter fw=new FileWriter("d:\\aaa\\b.mp4");
        char[] chars=new char[100];
        int length=-1;
        while((length=fr.read(chars))!=-1){
            fw.write(chars,0,length);
            fw.flush();
        }
        fw.close();
        fr.close();
    }

    private static void test08() throws IOException {
        // public BufferedWriter(Writer out)

//        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("a.txt")));
//        BufferedWriter bw=new BufferedWriter
//                (new OutputStreamWriter(
//                        new BufferedOutputStream(new FileOutputStream("a.txt"))));
        BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
        bw.write("aaaa");
        //换行
        bw.newLine();
        bw.write("bbb");
        bw.newLine();
        bw.write("ccc");
        bw.newLine();
        bw.flush();
        bw.close();


        BufferedReader br=new BufferedReader(new FileReader("a.txt"));
        String line=null;
        while((line=br.readLine())!=null){
            System.out.println(line);
        }
        br.close();
    }

    private static void test07() throws IOException {
        FileWriter fw = new FileWriter("a.txt");
        fw.write("abcd中文\n");
        fw.write("abcd中文\n");
        fw.write("abcd中文\n");
        fw.flush();
        fw.close();

        FileReader fr = new FileReader("a.txt");
        char[] mychars = new char[3];
        int length = -1;
        while ((length = fr.read(mychars)) != -1) {
            System.out.println(new String(mychars, 0, length));
        }
        fr.close();
    }

    private static void test06() throws IOException {
        OutputStreamWriter ows =
                new OutputStreamWriter
                        (new BufferedOutputStream(new FileOutputStream("a.txt")), "gbk");
        //字符流:一次写一个字符
        ows.write('a');
        ows.write(98);
        ows.write('中');
        ows.write('文');
        //一次写一个字符数组(字符串)
        ows.write("cd字符");
        //字符流清空缓冲区
        ows.flush();
        ows.close();

        InputStreamReader isr =
                new InputStreamReader(new BufferedInputStream(new FileInputStream("a.txt")), "gbk");
        //字符流：一次读一个字符数组
        char[] chars = new char[3];
        int length = -1;
        while ((length = isr.read(chars)) != -1) {
            System.out.println(new String(chars, 0, length));
        }
        isr.close();
    }

    private static void test05() throws IOException {
        //        InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"));
        InputStreamReader isr =
                new InputStreamReader(new BufferedInputStream(new FileInputStream("a.txt")));
        //字符流：一次读一个字符数组
        char[] chars = new char[3];
        int length = -1;
        while ((length = isr.read(chars)) != -1) {
            System.out.println(new String(chars, 0, length));
        }
        isr.close();
    }

    private static void test04() throws IOException {
        //        InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"));
        InputStreamReader isr =
                new InputStreamReader(new BufferedInputStream(new FileInputStream("a.txt")));
        //字符流：一次读一个字符
        int mychar = -1;
        while ((mychar = isr.read()) != -1) {
            System.out.println((char) mychar);
        }
        isr.close();
    }

    private static void test03() throws IOException {
        //        OutputStreamWriter ows=new OutputStreamWriter(new FileOutputStream("a.txt"));
        OutputStreamWriter ows =
                new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream("a.txt")));
        //字符流:一次写一个字符
        ows.write('a');
        ows.write(98);
        ows.write('中');
        ows.write('文');
        //一次写一个字符数组(字符串)
        ows.write("cd字符");
        //字符流清空缓冲区
        ows.flush();
        ows.close();
    }

    private static void test02() throws UnsupportedEncodingException {
        String str = "abc中文";
        //编码
        byte[] bytes = str.getBytes("utf-8");
        System.out.println(Arrays.toString(bytes));
        //解码
        System.out.println(new String(bytes, "utf-8"));
        System.out.println(new String(bytes, "gbk"));
        System.out.println(new String(bytes, "iso-8859-1"));
    }

    private static void test01() throws UnsupportedEncodingException {
        String str = "abc中文";
        //查看当前系统对应的编码utf-8:一个汉字是三个字节
        System.out.println(Arrays.toString(str.getBytes()));
        System.out.println(Arrays.toString(str.getBytes("utf-8")));
        //gbk:一个汉字是两个字节
        System.out.println(Arrays.toString(str.getBytes("gbk")));
    }
}
